package com.gaotong.science.controller;

import com.gaotong.science.util.R;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class Python1 {


      public static R runPython(String[] args) {
            Process proc;
            String message = "";
            try {
                  proc = Runtime.getRuntime().exec(args);// 执行py文件
                  //用输入输出流来截取结果
                  BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                  String line = null;

                  while ((line = in.readLine()) != null) {
                        System.out.println(line);
                        message = message + line;
                  }

                  in.close();
                  proc.waitFor();
                  if(message.contains("Success")){
                        return R.ok().message(message);
                  }
            } catch (IOException e) {
                  e.printStackTrace();
            } catch (InterruptedException e) {
                  e.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            }
            return R.error().message(message);
      }
}